package com.pool;

import java.io.IOException;
import java.net.URLEncoder;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.gps.domain.AlarmCacheData;
import org.gps.domain.TArea;
import org.gps.domain.TerminalData;
import org.tools.CircleAreaJudge;
import org.tools.LineAreaJudgeV2;
import org.tools.RectangleAreaJudge;

import com.dbmanager.DBManager;

// Referenced classes of package com.pool:
//			BatchSave

public class BatchSaveImpl
	implements BatchSave
{

	private Map areaLastTime;
	private Map waitTime;
	private Map isInAreaMap;
	static double PI = 3.1415926535897931D;
	static double x_pi = 52.359877559829883D;

	public BatchSaveImpl()
	{
		areaLastTime = new HashMap();
		waitTime = new HashMap();
		isInAreaMap = new HashMap();
	}

	public void saveList(List ls)
	{
		String insertPro;
		String callProc;
		CallableStatement pstmt;
		CallableStatement cstmt;
		Connection conn;
		Boolean flag = judgeAlarm(ls);
		if (flag) {
			insertPro = "{call PROC_ADD_LOCRECORD(?,?,?,?,?,?,?,?,?,?,?,?)}";
			callProc = "{call PROC_ADD_Last_LOC(?,?,?,?,?,?,?,?,?,?,?,?,?,?)}";
			pstmt = null;
			cstmt = null;
			conn = null;
			try
			{
				DBManager dbmanager = DBManager.getInstance();
				conn = dbmanager.getConnection();
				conn.setAutoCommit(false);
				pstmt = conn.prepareCall(insertPro);
				cstmt = conn.prepareCall(callProc);
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				for (int i = 0; i < ls.size(); i++)
				{
					TerminalData terminalDate = (TerminalData)ls.get(i);
					pstmt.setString(1, terminalDate.getImei());
					pstmt.setFloat(2, (float)terminalDate.getLongitude());
					pstmt.setFloat(3, (float)terminalDate.getLatitude());
					pstmt.setFloat(4, terminalDate.getSpeed());
					pstmt.setFloat(5, 0.0F);
					pstmt.setInt(6, terminalDate.getWay());
					pstmt.setFloat(7, 0.0F);
					pstmt.setFloat(8, 0.0F);
					try
					{
						pstmt.setTimestamp(9, new Timestamp(formatter.parse(terminalDate.getDiviceTime()).getTime()));
					}
					catch (ParseException e)
					{
						e.printStackTrace();
					}
					pstmt.setString(10, "1");
					pstmt.setInt(11, 0);
					pstmt.setString(12, "STATE");
					pstmt.addBatch();
					int grid_x = 1 + (int)(((terminalDate.getLongitude() + 180D) * 409600D) / 360D);
					int grid_y = 1 + (int)(((terminalDate.getLatitude() + 90D) * 409600D) / 180D);
					cstmt.setString(1, terminalDate.getImei());
					cstmt.setFloat(2, (float)terminalDate.getLongitude());
					cstmt.setFloat(3, (float)terminalDate.getLatitude());
					cstmt.setFloat(4, terminalDate.getSpeed());
					cstmt.setFloat(5, 0.0F);
					cstmt.setInt(6, terminalDate.getWay());
					cstmt.setFloat(7, 0.0F);
					cstmt.setFloat(8, 0.0F);
					try
					{
						cstmt.setTimestamp(9, new Timestamp(formatter.parse(terminalDate.getDiviceTime()).getTime()));
					}
					catch (ParseException e)
					{
						e.printStackTrace();
					}
					cstmt.setString(10, "1");
					cstmt.setInt(11, 0);
					cstmt.setString(12, "STATE");
					cstmt.setInt(13, grid_x);
					cstmt.setInt(14, grid_y);
					cstmt.addBatch();
				}

				pstmt.executeBatch();
				cstmt.executeBatch();
				conn.commit();
				conn.setAutoCommit(true);

			}
			catch (SQLException ex)
			{
				ex.printStackTrace();
			}finally{
				try {
					if(pstmt!=null){

						pstmt.close();
					}
					if(conn!=null){
						conn.close();
					}

				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
	}

	public void saveDataOntime(TerminalData terminalDate)
	{
		String insertPro;
		String callProc;
		CallableStatement pstmt;
		CallableStatement cstmt;
		Connection conn;
		insertPro = "{call PROC_ADD_LOCRECORD(?,?,?,?,?,?,?,?,?,?,?,?)}";
		callProc = "{call PROC_ADD_Last_LOC(?,?,?,?,?,?,?,?,?,?,?,?)}";
		pstmt = null;
		cstmt = null;
		conn = null;
		try
		{
			DBManager dbmanager = DBManager.getInstance();
			conn = dbmanager.getConnection();
			conn.setAutoCommit(false);
			pstmt = conn.prepareCall(insertPro);
			cstmt = conn.prepareCall(callProc);
			pstmt.setString(1, terminalDate.getImei());
			pstmt.setFloat(2, (float)terminalDate.getLongitude());
			pstmt.setFloat(3, (float)terminalDate.getLatitude());
			pstmt.setFloat(4, terminalDate.getSpeed());
			pstmt.setFloat(5, 0.0F);
			pstmt.setInt(6, terminalDate.getWay());
			pstmt.setFloat(7, 0.0F);
			pstmt.setFloat(8, 0.0F);
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try
			{
				pstmt.setTimestamp(9, new Timestamp(formatter.parse(terminalDate.getDiviceTime()).getTime()));
			}
			catch (ParseException e)
			{
				e.printStackTrace();
			}
			pstmt.setString(10, "1");
			pstmt.setInt(11, 0);
			pstmt.setString(12, "STATE");
			pstmt.addBatch();
			cstmt.setString(1, terminalDate.getImei());
			cstmt.setFloat(2, (float)terminalDate.getLongitude());
			cstmt.setFloat(3, (float)terminalDate.getLatitude());
			cstmt.setFloat(4, terminalDate.getSpeed());
			cstmt.setFloat(5, 0.0F);
			cstmt.setInt(6, terminalDate.getWay());
			cstmt.setFloat(7, 0.0F);
			cstmt.setFloat(8, 0.0F);
			try
			{
				cstmt.setTimestamp(9, new Timestamp(formatter.parse(terminalDate.getDiviceTime()).getTime()));
			}
			catch (ParseException e)
			{
				e.printStackTrace();
			}
			cstmt.setString(10, "1");
			cstmt.setInt(11, 0);
			cstmt.setString(12, "STATE");
			cstmt.addBatch();
			pstmt.executeBatch();
			cstmt.executeBatch();
			conn.commit();
			conn.setAutoCommit(true);
		
		}
		catch (SQLException ex)
		{
			ex.printStackTrace();
		}finally{
			try {        		
    			if(pstmt!=null){
    				
    				pstmt.close();
    			}
    			if(conn!=null){
    				conn.close();
    			}
    			
			  } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		}
	}

	public Boolean judgeAlarm(List ls)
	{
		Map map = AlarmCacheData.getInstance().getGpsAreaList();
		List allAreaList = AlarmCacheData.getInstance().getGpsAllAreaList();
		Boolean res = false;
		for (int i = 0; i < ls.size(); i++)
		{
			TerminalData terminalDate = (TerminalData)ls.get(i);
			double lat = terminalDate.getLatitude();
			double lng = terminalDate.getLongitude();
			double lnglat[] = getBaiduXy(lat, lng);
			lng = lnglat[0];
			lat = lnglat[1];
			int speed = terminalDate.getSpeed();
			String gpsTime = terminalDate.getDiviceTime();
			String deviceId = terminalDate.getImei();
			List areaList = (List)map.get(deviceId);
			if (speed > 30)
			{
				saveAlarmToDb(deviceId, lng, lat, speed, gpsTime, 1, "车辆超速", "当前速度："+speed+"km/h;限速："+30+"km/h", "");
				System.out.println("终端"+deviceId+"--车辆超速--"+"当前速度："+speed+"km/h;"+"限速："+30+"km/h");
			}
			if (areaList != null)
			{
				for (int j = 0; j < areaList.size(); j++)
				{
					TArea area = (TArea)areaList.get(j);
					String maxSpeed = area.getLimitSpeed();
					String maxTime = area.getLimitTime();
					boolean isInArea = false;
					if ("1".equals(area.getAreaLine()))
					{
						if ("1".equals(area.getAreaType()))
						{
							double radius = area.getRadius();
							String xys = area.getXys();
							String xy[] = xys.split(",");
							CircleAreaJudge CircleAreaJudge = new CircleAreaJudge(Double.parseDouble(xy[0]), Double.parseDouble(xy[1]), radius);
							isInArea = CircleAreaJudge.isInArea(lng, lat);
						} else
						if ("2".equals(area.getAreaType()))
						{
							String xys = area.getXys();
							RectangleAreaJudge rectangleArea = new RectangleAreaJudge(xys);
							isInArea = rectangleArea.isInArea(lng, lat);
						}
						if (isInArea)
						{
							saveAreaToDb(deviceId, lng, lat, speed, gpsTime, 4, area.getId().longValue(), area.getOrderId(), area.getAreaName());
							if (speed > Integer.parseInt(maxSpeed))
							{
								saveAlarmToDb(deviceId, lng, lat, speed, gpsTime, 1, "区域内超速", "当前速度："+speed+"km/h;"+area.getAreaName()+"区域限速："+maxSpeed+"km/h", area.getAreaName());
								System.out.println("终端"+deviceId+"--区域内超速--"+"当前速度："+speed+"km/h;"+area.getAreaName()+"区域限速："+maxSpeed+"km/h");
							}
							String AlarmRule = area.getAlarmRule();
							if ("1".equals(AlarmRule))
							{
								saveAlarmToDb(deviceId, lng, lat, speed, gpsTime, 4, "进区域报警", "进入"+area.getAreaName()+"报警", area.getAreaName());
								System.out.println("终端"+deviceId+"--进入区域报警--"+"进入"+area.getAreaName()+"报警");
							}
							String IsEmpty = area.getIsEmpty();
							String stockPile = area.getSticoPileId();
							String type = area.getAreaType();
							String areaName = area.getAreaName();
							saveVehicleFullemptyToDb(IsEmpty, deviceId, area.getId().toString(), stockPile, type, areaName);
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							try
							{
								Date befor_Time = sdf.parse(gpsTime);
								Long beforeTime = (Long)areaLastTime.get(deviceId+"_"+String.valueOf(area.getId()));
								if (beforeTime == null)
								{
									areaLastTime.put(String.valueOf(deviceId)+"_"+String.valueOf(area.getId()), Long.valueOf(befor_Time.getTime()));
								} else
								{
									Long time2t = Long.valueOf(befor_Time.getTime() - beforeTime.longValue());
									int mm = (int)(time2t.longValue() / 1000L / 60L);
									if (mm > Integer.valueOf(maxTime).intValue())
										if ("5".equals(type))
										{
											saveAlarmToDb(deviceId, lng, lat, speed, gpsTime, 8, "加水超时", String.valueOf(area.getAreaName())+"加水超时报警;加水："+mm+"分钟，限时："+maxTime+"分钟", area.getAreaName());
											System.out.println("终端"+deviceId+"--加水超时--"+area.getAreaName()+"加水站超时;停车："+mm+"分钟，限时："+maxTime+"分钟");
										} else
										{
											saveAlarmToDb(deviceId, lng, lat, speed, gpsTime, 7, "区域内停车超时", String.valueOf(area.getAreaName())+"任务区域停车超时报警;停车："+mm+"分钟，限时："+maxTime+"分钟", area.getAreaName());
											System.out.println("终端"+deviceId+"--区域内停车超时--"+area.getAreaName()+"任务区域停车超时;停车："+mm+"分钟，限时："+maxTime+"分钟");
										}
								}
							}
							catch (ParseException e)
							{
								e.printStackTrace();
							}
							isInAreaMap.put(String.valueOf(deviceId)+"_"+String.valueOf(area.getId()), Boolean.valueOf(true));
						} 
						else if (!isInAreaMap.isEmpty())
						{
							for (int m = 0; m < areaList.size(); m++)
							{
								TArea areaM = (TArea)areaList.get(m);
								if (isInAreaMap.containsKey(String.valueOf(deviceId)+"_"+String.valueOf(areaM.getId())) && m == j)
								{
									boolean flag = ((Boolean)isInAreaMap.get(String.valueOf(deviceId)+"_"+String.valueOf(areaM.getId()))).booleanValue();
									if (flag)
									{
										areaLastTime.put(String.valueOf(deviceId)+"_"+String.valueOf(areaM.getId()), null);
										isInAreaMap.put(String.valueOf(deviceId)+"_"+String.valueOf(areaM.getId()), Boolean.valueOf(false));
										if ("2".equals(areaM.getAlarmRule()))
										{
											saveAlarmToDb(deviceId, lng, lat, speed, gpsTime, 4, "出区域报警", "离开"+area.getAreaName()+"报警", area.getAreaName());
											System.out.println("终端"+deviceId+"--出区域报警--"+"出"+area.getAreaName()+"报警");
										}
									}
								}
							}

						}
					} else
					if ("2".equals(area.getAreaLine()))
					{
						String linePoint = area.getXys();
						LineAreaJudgeV2 line = new LineAreaJudgeV2(linePoint, "100");
						boolean isOutLine = line.isInArea(String.valueOf(lng), String.valueOf(lat));
						if (!isOutLine)
						{
							saveAlarmToDb(deviceId, lng, lat, speed, gpsTime, 2, "偏离任务线路", "偏离"+area.getAreaName()+"任务线路报警", area.getAreaName());
							System.out.println("终端"+deviceId+"--偏离任务线路--"+area.getAreaName());
						}
					}
				}
				res = true;
			} else {
				if (allAreaList!= null && allAreaList.size() > 0){
					for (int j = 0; j < allAreaList.size(); j++) {
						TArea area = (TArea) allAreaList.get(j);
						if (!res) {
							if ("1".equals(area.getAreaLine())) {
								if ("1".equals(area.getAreaType())) {
									double radius = area.getRadius();
									String xys = area.getXys();
									String xy[] = xys.split(",");
									CircleAreaJudge CircleAreaJudge = new CircleAreaJudge(Double.parseDouble(xy[0]), Double.parseDouble(xy[1]), radius);
									res = CircleAreaJudge.isInArea(lng, lat);
								} else if ("2".equals(area.getAreaType())) {
									String xys = area.getXys();
									RectangleAreaJudge rectangleArea = new RectangleAreaJudge(xys);
									res = rectangleArea.isInArea(lng, lat);
								}
							}
						}
						if (res) {
							//插入运料记录
							Long areaId = area.getId();
							String areaName = area.getAreaName();
							String materialName = area.getSticoPileId();
							DBManager dbmanager = DBManager.getInstance();
							Connection conn = dbmanager.getConnection();
							PreparedStatement st = null;
							String sb ="insert into g_vehicle_history_material (DEVICE_ID,SCHEDUAREAID,SCHEDUAREANAME,MATERIALNAME,MATERIALTYPE,REMARK)" +
									" values(" + deviceId + "," + areaId + "," + areaName + "," + materialName+ "," + materialName + "," + "系统调度)";
							try {
								st = conn.prepareStatement(sb, 1005, 1007);
								Integer tmp = st.executeUpdate();
								if (tmp != 1){
									System.out.println("未绑定责任区的车辆插入运料记录失败");
								}
							} catch (SQLException e) {
								System.out.println(e.getMessage());
							} finally {
								try {
									if(st!=null){

										st.close();
									}
									if(conn!=null){
										conn.close();
									}
								} catch (SQLException e) {
									e.printStackTrace();
								}
							}
						}
					}
				}
			}
		}
		return res;
	}

	public void saveAlarmToDb(String deviceId, double lng, double lat, int speed, String gpsTime, 
			int overspeed, String alarmName, String alarmDesc, String alarmAreaName)
	{
		String insertALarmPro;
		CallableStatement pstmt;
		Connection conn;
		insertALarmPro = "{call PROC_ADD_ALARM(?,?,?,?,?,?,?,?,?,?,?,?,?,?)}";
		pstmt = null;
		conn = null;
		try
		{
			DBManager dbmanager = DBManager.getInstance();
			conn = dbmanager.getConnection();
			conn.setAutoCommit(false);
			pstmt = conn.prepareCall(insertALarmPro);
			pstmt.setString(1, deviceId);
			pstmt.setFloat(2, (float)lng);
			pstmt.setFloat(3, (float)lat);
			pstmt.setFloat(4, speed);
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try
			{
				pstmt.setTimestamp(5, new Timestamp(formatter.parse(gpsTime).getTime()));
				pstmt.setFloat(6, overspeed);
				pstmt.setString(7, alarmName);
				pstmt.setString(8, alarmDesc);
				pstmt.setString(9, alarmAreaName);
				pstmt.registerOutParameter(10, 12);
				pstmt.registerOutParameter(11, 12);
				pstmt.registerOutParameter(12, 12);
				pstmt.registerOutParameter(13, 12);
				pstmt.registerOutParameter(14, 12);
			}
			catch (ParseException e)
			{
				e.printStackTrace();
			}
			pstmt.execute();
			conn.commit();
			String tels = pstmt.getString(10);
			String flag = pstmt.getString(11);
			String vihicleName = pstmt.getString(12);
			String dName = pstmt.getString(13);
			String dTel = pstmt.getString(14);
			if ("1".equals(flag))
			{
				String telsId[] = tels.split(",");
				for (int i = 0; i < telsId.length; i++)
					sendMsg(telsId[i], vihicleName, alarmName, gpsTime, alarmDesc, deviceId, dName, dTel);

			}	
		}
		catch (SQLException ex)
		{
			ex.printStackTrace();
		}finally{
			try {        		
    			if(pstmt!=null){
    				
    				pstmt.close();
    			}
    			if(conn!=null){
    				conn.close();
    			}
    			
			  } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		}
		
	}

	public void saveVehicleFullemptyToDb(String IsEmpty, String deviceId, String areaId, String stockPile, String areaType, String areaName)
	{
		String insertALarmPro;
		CallableStatement pstmt;
		Connection conn;
		insertALarmPro = "{call PROC_ADD_Vehicle_Fullempty(?,?,?,?,?,?)}";
		pstmt = null;
		conn = null;
		try
		{
			DBManager dbmanager = DBManager.getInstance();
			conn = dbmanager.getConnection();
			conn.setAutoCommit(false);
			pstmt = conn.prepareCall(insertALarmPro);
			pstmt.setString(1, IsEmpty);
			pstmt.setString(2, deviceId);
			pstmt.setString(3, areaId);
			pstmt.setString(4, stockPile);
			pstmt.setString(5, areaType);
			pstmt.setString(6, areaName);
			pstmt.addBatch();
			pstmt.executeBatch();
			conn.commit();
			conn.setAutoCommit(true);
		}
		catch (SQLException ex)
		{
			ex.printStackTrace();
		}
		finally{
			try {        		
    			if(pstmt!=null){
    				
    				pstmt.close();
    			}
    			if(conn!=null){
    				conn.close();
    			}
    			
			  } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		}
		
	}

	public void saveAreaToDb(String deviceId, double lng, double lat, int speed, String gpsTime, 
			int inArea, long areaId, int orderId, String areaName)
	{
		String insertALarmPro;
		CallableStatement pstmt;
		Connection conn;
		insertALarmPro = "{call PROC_ADD_AREA_ALARM(?,?,?,?,?,?,?,?,?,?,?,?,?,?)}";
		pstmt = null;
		conn = null;
		try
		{
			DBManager dbmanager = DBManager.getInstance();
			conn = dbmanager.getConnection();
			conn.setAutoCommit(false);
			pstmt = conn.prepareCall(insertALarmPro);
			pstmt.setString(1, deviceId);
			pstmt.setFloat(2, (float)lng);
			pstmt.setFloat(3, (float)lat);
			pstmt.setFloat(4, speed);
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try
			{
				pstmt.setTimestamp(5, new Timestamp(formatter.parse(gpsTime).getTime()));
				pstmt.setFloat(6, inArea);
				pstmt.setFloat(7, areaId);
				pstmt.setInt(8, orderId);
				pstmt.setString(9, areaName);
				pstmt.registerOutParameter(10, 12);
				pstmt.registerOutParameter(11, 12);
				pstmt.registerOutParameter(12, 12);
				pstmt.registerOutParameter(13, 12);
				pstmt.registerOutParameter(14, 12);
			}
			catch (ParseException e)
			{
				e.printStackTrace();
			}
			pstmt.execute();
			conn.commit();
			String tels = pstmt.getString(10);
			String flag = pstmt.getString(11);
			String vihicleName = pstmt.getString(12);
			String dName = pstmt.getString(13);
			String dTel = pstmt.getString(14);
			System.out.println("发送的手机号码======================="+tels);
			if ("1".equals(flag))
			{
				String telsId[] = tels.split(",");
				for (int i = 0; i < telsId.length; i++)
					sendMsg(telsId[i], vihicleName, "偏离任务区域", gpsTime, String.valueOf(areaName)+"中偏离任务区域", deviceId, dName, dTel);

			}
			conn.setAutoCommit(true);
			
		}
		catch (SQLException ex)
		{
			ex.printStackTrace();
		}
		finally{
			try {        		
    			if(pstmt!=null){
    				
    				pstmt.close();
    			}
    			if(conn!=null){
    				conn.close();
    			}
    			
			  } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		}
	}

	public static void sendSMSToDb(String tel, String vihicleName, String alarmName, String gpsTime, String alarmDesc, String deviceId)
	{
		String insertALarmPro;
		CallableStatement pstmt;
		Connection conn;
		insertALarmPro = "{call PROC_ADD_SMS_TO_DB(?,?,?,?,?,?)}";
		pstmt = null;
		conn = null;
		try
		{
			DBManager dbmanager = DBManager.getInstance();
			conn = dbmanager.getConnection();
			conn.setAutoCommit(false);
			pstmt = conn.prepareCall(insertALarmPro);
			pstmt.setString(1, tel);
			pstmt.setString(2, vihicleName);
			pstmt.setString(3, alarmName);
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try
			{
				pstmt.setTimestamp(4, new Timestamp(formatter.parse(gpsTime).getTime()));
			}
			catch (ParseException e)
			{
				e.printStackTrace();
			}
			pstmt.setString(5, alarmDesc);
			pstmt.setString(6, deviceId);
			pstmt.addBatch();
			pstmt.executeBatch();
			conn.commit();
			conn.setAutoCommit(true);
	
		}
		catch (SQLException ex)
		{
			ex.printStackTrace();
		}finally{
			try {        		
    			if(pstmt!=null){
    				
    				pstmt.close();
    			}
    			if(conn!=null){
    				conn.close();
    			}
    			
			  } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		}
	}

	private static double[] getBaiduXy(double lat, double lon)
	{
		double xy[] = new double[2];
		double a = 6378245D;
		double ee = 0.0066934216229659433D;
		double dLat = transformLat(lon - 105D, lat - 35D);
		double dLon = transformLon(lon - 105D, lat - 35D);
		double radLat = (lat / 180D) * PI;
		double magic = Math.sin(radLat);
		magic = 1.0D - ee * magic * magic;
		double sqrtMagic = Math.sqrt(magic);
		dLat = (dLat * 180D) / (((a * (1.0D - ee)) / (magic * sqrtMagic)) * PI);
		dLon = (dLon * 180D) / ((a / sqrtMagic) * Math.cos(radLat) * PI);
		double x = lon + dLon;
		double y = lat + dLat;
		double z = Math.sqrt(x * x + y * y) + 2.0000000000000002E-005D * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) + 3.0000000000000001E-006D * Math.cos(x * x_pi);
		double bdLon = z * Math.cos(theta) + 0.0064999999999999997D;
		double bdLat = z * Math.sin(theta) + 0.0060000000000000001D;
		System.out.println(String.valueOf(bdLon)+bdLat);
		xy[0] = bdLon;
		xy[1] = bdLat;
		return xy;
	}

	private static double transformLat(double x, double y)
	{
		double ret = -100D + 2D * x + 3D * y + 0.20000000000000001D * y * y + 0.10000000000000001D * x * y + 0.20000000000000001D * Math.sqrt(Math.abs(x));
		ret += ((20D * Math.sin(6D * x * PI) + 20D * Math.sin(2D * x * PI)) * 2D) / 3D;
		ret += ((20D * Math.sin(y * PI) + 40D * Math.sin((y / 3D) * PI)) * 2D) / 3D;
		ret += ((160D * Math.sin((y / 12D) * PI) + 320D * Math.sin((y * PI) / 30D)) * 2D) / 3D;
		return ret;
	}

	private static double transformLon(double x, double y)
	{
		double ret = 300D + x + 2D * y + 0.10000000000000001D * x * x + 0.10000000000000001D * x * y + 0.10000000000000001D * Math.sqrt(Math.abs(x));
		ret += ((20D * Math.sin(6D * x * PI) + 20D * Math.sin(2D * x * PI)) * 2D) / 3D;
		ret += ((20D * Math.sin(x * PI) + 40D * Math.sin((x / 3D) * PI)) * 2D) / 3D;
		ret += ((150D * Math.sin((x / 12D) * PI) + 300D * Math.sin((x / 30D) * PI)) * 2D) / 3D;
		return ret;
	}

	public static void sendMsg(String tel, String vihicleName, String alarmName, String gpsTime, String alarmDesc, String deviceId, String dName, String dTel)
	{
		HttpClient client = new HttpClient();
		System.out.println("手机号码---------------------"+tel);
		try
		{
			String url = "http://v.juhe.cn/sms/send";
			PostMethod post = new PostMethod(url);
			post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			NameValuePair data[] = {new NameValuePair("tpl_id","19082"), new NameValuePair("key", "60f0212de3ccad1534288a0f988e953c"), new NameValuePair("mobile", tel), new NameValuePair("tpl_value", URLEncoder.encode("#code#="+vihicleName+alarmName+";报警时间："+gpsTime+";描述："+alarmDesc+";驾驶员姓名："+dName+"驾驶员电话："+dTel, "UTF-8"))};
			post.setRequestBody(data);
			client.executeMethod(post);
			org.apache.commons.httpclient.Header headers[] = post.getResponseHeaders();
			int statusCode = post.getStatusCode();
			System.out.println("statusCode:"+statusCode);
			if (statusCode == 200)
				sendSMSToDb(tel, vihicleName, alarmName, gpsTime, alarmDesc, deviceId);
			String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
			System.out.println(result);
			post.releaseConnection();
		}
		catch (HttpException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public static void main(String args1[])
	{
	}

}
